package com.shouhuyun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.shouhuyun.dao.RememberDao;
import com.shouhuyun.service.ReMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


//会员服务
@Service(interfaceClass = ReMemberService.class)
@Transactional
public class RememberServicelmpl implements ReMemberService {
    @Autowired
    private RememberDao rememberDao;

    @Override
    public List<Map<String, Object>> findReportCount() {
        return rememberDao.findRememberCount();
    }


    @Override
    public List<Map<String, Object>> findReportCountById() {
        // 查出身份证
        List<String> id = rememberDao.findIdcard();
        try {
            List<Long> ages = new ArrayList<Long>();
            for (String s : id) {
                //截取日期数字
                String substring = s.substring(6, 14); // 拿出年月日
                //截取的数字转换成日期
                Date birthday = new SimpleDateFormat("yyyyMMdd").parse(substring); // 19951011
                //当前日期  毫秒值
                long now = System.currentTimeMillis();
                long birth = birthday.getTime();
                long l = now - birth;
                long age = l / 1000 / 60 / 60 / 24 / 365;
                ages.add(age);
            }

            ArrayList<Long> age1 = new ArrayList<>();
            ArrayList<Long> age2 = new ArrayList<>();
            ArrayList<Long> age3 = new ArrayList<>();
            ArrayList<Long> age4 = new ArrayList<>();
            for (Long age : ages) {
                if (age < 18) {
                    age1.add(age);
                } else if (age >= 18 && age < 30) {
                    age2.add(age);
                } else if (age >= 30 && age < 45) {
                    age3.add(age);
                } else {
                    age4.add(age);
                }
            }
            // 一个list集合 存放四个年龄区间
            List<Map<String,Object>> list = new ArrayList<>();

            Map<String, Object> map1 = new HashMap<>();
            map1.put("value",age1.size());
            map1.put("name","0岁到18岁");
            list.add(map1);

            Map<String, Object> map2 = new HashMap<>();
            map2.put("value",age2.size());
            map2.put("name","18岁到30岁");
            list.add(map2);

            Map<String, Object> map3 = new HashMap<>();
            map3.put("value",age3.size());
            map3.put("name","30岁到45岁");
            list.add(map3);

            Map<String, Object> map4 = new HashMap<>();
            map4.put("value",age4.size());
            map4.put("name","45岁以上");
            list.add(map4);

            return list;
            /**
             *  List<Map>   ===>     Map<String,Object>
             */
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
}

